package org.example.link_list;

public class Delete_the_nth_to_last_node_of_the_linked_list {
    public static void main(String[] args) {
        //删除链表的倒数第 N 个结点
        //给你一个链表，删除链表的倒数第 n 个结点，并且返回链表的头结点。

        ListNode listNode2 = new ListNode(5);
        listNode2.next = null;
        ListNode listNode3 = new ListNode(4,listNode2);
        ListNode listNode4 = new ListNode(3,listNode3);
        ListNode listNode5 = new ListNode(2,listNode4);
        ListNode head = new ListNode(1,listNode5);
        int n = 1;
        int m = n-1;
        ListNode after = head;
        ListNode current = head;
        ListNode before = head;
        while(after.next!=null){
            if (m!=0){
                m--;
                after = after.next;
            }else{
                after = after.next;
                before = current;
                current = current.next;
            }
        }
        if (current!=head){
            before.next = current.next;
            current.next = null;
        }else{
            head = current.next;
            current.next = null;
        }
        ListNode w = head;
        while (w !=null ){
            System.out.print(w.val);
            w = w.next;
        }
    }
}
